// pages/mechanism/mechanism.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collect:false,
    tabindex:0,
    tablist:["问题","文章","专家","套餐","产品"],
    show:false,   //显示简介,
    answer:[],
    content:[],
    page:0,
    taocan:[],
    zhuanjia:[],
    goods:[]
  },
  // 查看医院简介
  
  //切换
  bindtab(e){
    var that = this
    var index = e.currentTarget.dataset.index
    that.setData({
      tabindex:index,
      answer:[],
      content:[],
      taocan:[],
      zhuanjia:[],
      goods:[],
      page:0
    })    
    if(index == 0)
    {
      that.getAnswer();
    }else if(index == 1)
    {
      that.getcontent();
    }else if(index == 2)
    {
      that.getzhuanjia();
    }else if(index == 3)
    {
      that.gettaocan();
    }else if(index == 4)
    {
      that.getgoods()
    }
  }, 
  searchbtn() {
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/search/search',
    })
  },
  // 显示
  bindshow(e){
    var that = this
    var show = that.data.show
    show = !show
    that.setData({
      show:show
    })
  },
  tozhuanjia:function(e){
    
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid='+e.currentTarget.dataset.zid,
    })
  },
  allanswer:function(){
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblems/publicProblems',
    })
  },
  allzhuanjia:function(){
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/changeDoctor/changeDoctor?come=jigouzhuye&hid='+this.data.hid,
    })
  },
  allkeshi:function(){
    
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/faxian/faxian?hid='+this.data.hid,
    })
  },
  // 问题详情
  bindposition(e){
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblemsInfor/publicProblemsInfor?id='+e.currentTarget.dataset.id+'&typs=answer&state=1',
    })
    console.log(e);
    this.getAnswer();
  },
  // 文章详情
  bindarticle(e){
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq?id='+e.currentTarget.dataset.id+'&p_id='+e.currentTarget.dataset.p_id+'&zid='+e.currentTarget.dataset.zid,
    })
  },
  // 专家详情
  bindexpert(e){
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid='+e.currentTarget.dataset.zid,
    })
  },
  // 套餐详情
  bindmeal(e){
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/tijian/detail/detail?tid='+e.currentTarget.dataset.tid,
    })
  },
  // 产品详情
  bindproduct(e){
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/product/product?sid='+e.currentTarget.dataset.sid+'fid=',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var hid = options.hid;
    that.setData({
      hid:hid
    })
    that.getHospital();
    that.getzhuanjia();
    app.util.request({
      'url': 'entry/wxapp/zixun.getalluser',
      success: function (res) {
        that.setData({
          rew: res.data
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        that.setData({
          baseinfo: res.data
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },
  toaddress:function(e){
    console.log('导航跳转',e.currentTarget.dataset)
    
    wx.openLocation({
      latitude: parseFloat(e.currentTarget.dataset.latitude), // 纬度，范围为-90~90，负数表示南纬
      longitude: parseFloat(e.currentTarget.dataset.longitude), // 经度，范围为-180~180，负数表示西经
      name:e.currentTarget.dataset.name,
      scale: 28, // 缩放比例
    })
    
  },
  collect: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/user.changelove',
      data: {
        hid: that.data.hid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 9
      },
      success: function (res) {
        console.log(res)
      }
    })
    this.setData({
      collect: !this.data.collect
    })
    if (this.data.collect == true) {
      wx.showToast({
        title: '收藏成功',
        icon: 'none',
      })
    } else {

      wx.showToast({
        title: '取消收藏成功',
        icon: 'none',
      })
    }
  },
  // 获取医院详情
  getHospital:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospital_details",
      data:{
        hid:that.data.hid,
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        that.setData({
          info:res.data,
          hid:res.data.hid,
          collect:res.data.collect
        })
        that.getAnswer()
      }
    })
  },
  // 获取问题列表
  getAnswer:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospital_answer",
      data:{
        hid:that.data.hid,
        page:that.data.page
      },
      success:function(res)
      {
        var page = that.data.page + 1;
        that.setData({
          answer:that.data.answer.concat(res.data),
          page:page
        })
        console.log("问题列表",res)
      }
    })
  },
  // 获取文章列表
  getcontent:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospotal_content",
      data:{
        hid:that.data.hid,
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          content:that.data.content.concat(res.data),
          page:page
        })
        console.log("文章列表",res)
      }
    })
  },
   // 获取专家列表
   getzhuanjia:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospital_zhuanjia",
      data:{
        hid:that.data.hid,
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          zhuanjia:that.data.zhuanjia.concat(res.data),
          page:page
        })
        console.log("专家",res)
      }
    })
  },
  // 获取套餐列表
  gettaocan:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospital_taocan",
      data:{
        hid:that.data.hid,
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          taocan:that.data.taocan.concat(res.data),
          page:page
        })
      }
    })
  },
   // 获取产品列表
   getgoods:function(){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/user.hospital_goods",
      data:{
        hid:that.data.hid,
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page + 1;
        that.setData({
          goods:that.data.goods.concat(res.data),
          page:page
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var index = that.data.tabindex;
    if(index == 0)
    {
      that.getAnswer();
    }else if(index == 1)
    {
      that.getcontent();
    }else if(index == 2)
    {
      that.getzhuanjia();
    }else if(index == 3)
    {
      that.gettaocan();
    }else if(index == 4)
    {
      that.getgoods()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var that = this;

    return {
      title:that.data.info.agentname,
      path: '',
      imageUrl: '',
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})